package com.wewoo.log4e.starter.spring.boot.autoconfigure;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.wewoo.log4e.core.spring.boot.autoconfigure.Log4eProperties;
import com.wewoo.log4e.core.util.ReadLogUtil;
import com.wewoo.log4e.starter.action.ReadLogDataBaseUtil;
import com.wewoo.log4e.starter.executor.DataSourceExecutor;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @ClassName DataSourceLogConfiguration
 * @Description TODO
 * @Author tangjin
 * @Date 16:57 2022/2/14
 **/
@Configuration
public class DataSourceLogAutoConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "log4e.datasource")
    public DataSource log4eDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    JdbcTemplate jdbcTemplate(@Qualifier("log4eDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    @Order(200)
    public DataSourceExecutor dataSourceExecutor(JdbcTemplate jdbcTemplate) {
        return new DataSourceExecutor(jdbcTemplate);
    }

    @Bean
    public ReadLogDataBaseUtil readLogDataBaseUtil(JdbcTemplate jdbcTemplate){
        return ReadLogDataBaseUtil.builder().jdbcTemplate(jdbcTemplate).build();
    }
}
